﻿using Authority.Logic;
using Microsoft.AspNetCore.Mvc;
using Web.Manage.App_Start;
using System.Linq;
using Authority.Model;
using System.Collections.Generic;
using Tool.Model;
using System;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Hosting;
using Network.Library;
using Ioc.Library;

namespace Web.Manage.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class HomeController : BaseController
    {

        private IHostingEnvironment hostingEnv;

        public HomeController(IHostingEnvironment env)
        {
            this.hostingEnv = env;
        }

        #region 界面
        [UserAuthorization(true, 1)]
        public ActionResult Index()
        {
            var loginUser = UserSession.GetLoginUser<AdminTicket>();
            {
                if (loginUser!=null)
                {
                    ISysUserService sysUserService = UnityCIContainer.Instance.GetService<ISysUserService>();
                    var userInfo = sysUserService.LoadUserByLoginName(loginUser.LoginName);
                    if (userInfo.Code == ActionCodes.Success)
                    {
                        ViewBag.LoginUserAccount = userInfo.Data.sLoginName;
                        ViewBag.LoginUserName = userInfo.Data.sUserName;
                        ViewBag.PicUrl = userInfo.Data.sPicUrl;

                        loginUser.LoginName = userInfo.Data.sLoginName;
                        loginUser.UserName = userInfo.Data.sUserName;
                        loginUser.PicUrl = userInfo.Data.sPicUrl;

                        UserSession.AddLoginUser(loginUser);
                    }
                    else
                    {
                        ViewBag.LoginUserAccount = loginUser.LoginName;
                        ViewBag.LoginUserName = loginUser.UserName;
                        ViewBag.PicUrl = loginUser.PicUrl;
                    }

                    var sysMenu = UnityCIContainer.Instance.GetService<ISysMenuService>();

                    ViewBag.Menus = sysMenu.LoadMenusByRoles().Data.ToList().FindAll(p => p.iLevel == (int)MenuLevel.Second);
                }
                else
                {
                    return Redirect("/Admin/Login/Index");
                }
            }
            return View();
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, 1)]
        public ActionResult HomeMain()
        {
            return View();
        }

        /// <summary>
        /// 用户信息界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, 1)]
        public ActionResult UserInfo()
        {
            return View();
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, 1)]
        public ActionResult UploadImg()
        {
            return View();
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, 1)]
        public ActionResult ChangePwd()
        {
            return View();
        }
        #endregion

        #region 接口
        /// <summary>
        /// 获取左侧菜单信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, 2)]

        public ContentResult LoadSubMenus()
        {
            var resultInfo = new ResultJsonInfo<List<MenuInfo>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<string>>();

                var sysMenu = UnityCIContainer.Instance.GetService<ISysMenuService>();

                resultInfo = sysMenu.LoadSubMenus(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }

            return ReturnResult(resultInfo);
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, 2)]
        [EnableCors("AllowSpecificOrigin")]
        public ActionResult UploadImage()
        {
            var resultInfo = new ResultJsonInfo<string>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<Dictionary<string, object>>>();

                ISysUserService sysUserService = UnityCIContainer.Instance.GetService<ISysUserService>();

                resultInfo = sysUserService.UploadImage(hostingEnv.WebRootPath, parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnResult(resultInfo);
        }

        /// <summary>
        /// 保存用户信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, 2)]
        public ActionResult SaveUserInfo()
        {
            var resultInfo = new ResultJsonInfo<SysUserEntity>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<UserInfoModifyRequest>>();

                ISysUserService sysUserService = UnityCIContainer.Instance.GetService<ISysUserService>();

                resultInfo = sysUserService.SaveUserInfo(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnResult(resultInfo);
        }


        /// <summary>
        /// 确认用户修改密码
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, 2)]
        public ActionResult SaveChangePwd()
        {
            var resultInfo = new ResultJsonInfo<bool>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<UserPswModifyRequest>>();

                ISysUserService sysUserService = UnityCIContainer.Instance.GetService<ISysUserService>();

                var  userInfo= UserSession.GetLoginUser<AdminTicket>();

                parametersInfo.parameters.Code = userInfo.Code;

                resultInfo = sysUserService.SaveChangePwd(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnResult(resultInfo);
        }

        #endregion

    }
}